package com.zjut.zs.exvalue.manager;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.zjut.an.util.module.PageModule;
import com.zjut.zs.cj.dao.ZsCj;
import com.zjut.zs.exvalue.dao.ZsExValue;
import com.zjut.zs.exvalue.dao.ZsExValueDAO;
import com.zjut.zs.user.dao.ZsUser;
import com.zjut.zs.util.tools.DataCount;
import com.zjut.zs.xy.dao.ZsXy;


public class ZsExValueManager {

	@Resource(name = "ZsExValueDAO")
	private ZsExValueDAO dao;
	
	public void addData(ZsExValue obj) {	
		dao.save(obj);
	}
	
	public void updateData(ZsExValue obj) {
		dao.update(obj);
	}
	
	public void deleteData(ZsExValue obj) {
		dao.delete(obj);
	}

	public void deleteDatas(List<ZsExValue> objlist) {
		for (ZsExValue ZsExValue : objlist) {
			dao.delete(ZsExValue);
		}
	}
	
	public PageModule getDataByInstance (int pageNumber,int pageSize, String sort, String order, ZsExValue obj) {
		return dao.findByInstance(pageNumber, pageSize, sort, order, obj);
	}
	
	public List getDatas() {
		return dao.findAll();
	}

	public ZsExValue getData(String objId) {
		return dao.findById(objId);
	}
	
	public List getDataGroup(String column) {
		
		String hql = "select "+column+", count(exvId) from ZsExValue group by "+column;
		
		System.out.println("@@@@"+hql);
		
		List<Object[]> list = dao.findByHql(1, 1000, hql, null);
		
		List objlist = new ArrayList();
		for(Object[] object : list){ 
            DataCount count = new DataCount();
            count.setName((String)object[0]);
            count.setValue(((Long)object[1]).intValue());
            objlist.add(count);
        } 
		return objlist;
	}

	public ZsExValue getDataByUser(ZsUser user) {
		ZsExValue obj = new ZsExValue();
		obj.setZsUser(user);
		if(this.getDataByInstance(1, 1, null, null, obj).getObjects().size()>0) {
			return (ZsExValue)this.getDataByInstance(1, 1, null, null, obj).getObjects().get(0);
		} else{
			return obj;
		}
	}
}
